﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MTC.Common.Enum;
using MTC.Web.Business;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class Rosters : System.Web.UI.Page
    {
        private ScheduleEntity _entity;
        Business.Schedule ScheduleBO = new Business.Schedule();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the driver id if available
                if (Request.Params[Params.SHIFTID] != "0")
                {
                }
            }
        }

        private void SetupEnvironment()
        {
            CboShift.DataSource = ShiftType.GetShiftTypes();
            CboShift.DataBind();
        }

        private void LoadFields()
        {
            if (_entity == null)
                return;

        }

        private void ClearFields()
        {
        }

        private bool SetFields()
        {
            return true;
        }


        protected bool ValidateFields()
        {
            return true;
        }

        protected void BtnSelect_Click(object sender, EventArgs e)
        {
            if (TxtDateofSchedule.Text.Length == 0)
                return;
            DateTime dateOfSchedule = Convert.ToDateTime(TxtDateofSchedule.Text);
           // ASPxGridView1.DataSource = ScheduleBO.GetASchedule(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value));

           if (RosterExist(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value)))
            {
                LoadRoster(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value));
            }
           else
            {
                CreateRoster(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value));
            }
        }

        private void CreateRoster(DateTime schedule, int shift)
        {
            ScheduleBO.CreateASchedule(schedule, Convert.ToInt32(CboShift.SelectedItem.Value));
            grdRosters.DataBind();
        }

        private void LoadRoster(DateTime schedule, int shift)
        {
            grdRosters.DataBind();
        }

        private bool RosterExist(DateTime schedule, int shift)
        {
            return ScheduleBO.CheckSchedule(schedule, shift);
        }

        protected void BtnSetupRoster_Click(object sender, EventArgs e)
        {
            if (TxtDateofSchedule.Text.Length == 0)
                return;
            DateTime dateOfSchedule = Convert.ToDateTime(TxtDateofSchedule.Text);
        }


        protected void btnExcel_Click(object sender, ImageClickEventArgs e)
        {
            GrdExport.WriteXlsToResponse();
        }

        protected void btnPdf_Click(object sender, ImageClickEventArgs e)
        {
            GrdExport.WritePdfToResponse();
        }

        protected void btnRtf_Click(object sender, ImageClickEventArgs e)
        {
            GrdExport.WriteRtfToResponse();
        }

        protected void BtnClear_Click(object sender, EventArgs e)
        {
            if (TxtDateofSchedule.Text.Length == 0)
                return;
            DateTime dateOfSchedule = Convert.ToDateTime(TxtDateofSchedule.Text);
            if (ScheduleBO.DeleteASchedule(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value)))
            {
                ScheduleBO.GetASchedule(dateOfSchedule, Convert.ToInt32(CboShift.SelectedItem.Value));
                grdRosters.DataBind();
            }

        }


    }
}
